﻿using System.Globalization;
using Microsoft.Win32;
using System.Diagnostics;
namespace AnalyStock.DataManage;
internal static class CommAppMethod
{
    /// <summary>
    ///     提示信息方法通用处理,链式方法使用模式
    /// </summary>
    /// <param name="message"></param>
    public static void ErrorMessage(this string message)
    {
        MessageBox.Show(message, "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
    }

    public static void NotifyMessage(this string message)
    {
        MessageBox.Show(message, "完成提示", MessageBoxButton.OK, MessageBoxImage.Information);
    }

    public static bool IsConfirmMessage(this string strSuggestion)
    {
        return MessageBox.Show(strSuggestion, "确认操作",
            MessageBoxButton.OKCancel, MessageBoxImage.Question) is MessageBoxResult.OK;
    }

    /// <summary>
    ///     可迭代集合NUll，Empty判断
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> list) where T : class
    {
        return list is null || !list.Any(); // !list?.Any() ?? true;
    }
    /*public static bool IsNullThrowException<T>(this T t)
    {
        if (t == null)
        {
            return true;
            throw new ArgumentNullException($"{t} is null");
        }
        return false;
    }*/

    /// <summary>
    ///     手工清除已经填充、可放弃使用的集合数据，释放内存。该方法因系统内存管理机制存在质疑！
    /// </summary>
    /// <param name="list"></param>
    /// <typeparam name="T"></typeparam>
    public static void Dispose<T>(this ICollection<T> list)
    {
        list?.Clear();
    }

    /// <summary>
    ///     "yyyyMMdd"字符格式转换为日期格式；
    /// </summary>
    /// <param name="strDate"></param>
    /// <returns></returns>
    public static DateTime ConvertToDate(this string strDate)
    {
        return DateTime.ParseExact(
            strDate,
            "yyyyMMdd",
            CultureInfo.CurrentCulture
        );
        //return DateTime.Parse($"{strDate[0..4]}/{strDate[4..6]}/{strDate[6..8]}");
    }
    //转成常用日期格式"yyyyMMdd"
    public static string ToDateString(this DateTime date)
    {
        return date.ToString("yyyyMMdd");
    }
    //日期字符加分割符号"YYYY-MM-DD"
    public static string ToDateStrWithGapSymbols(this string date)
    {
        return string.Join("-", date[..4], date[4..6], date[6..8]);
    }

    /// <summary>
    ///     转换为只有四位小数位的浮点数
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static float ConvertToFourPoints(this float input)
    {
        return float.Round(input, 4);
    }

    public static float ConvertToFloat(this string input)
    {
        return string.IsNullOrWhiteSpace(input) ? 0.0f : Single.Parse(input);
    }

    /// <summary>
    /// 打开同花顺i问网页
    /// </summary>
    /// <param name="stockName"></param>
    public static void OpenNetByBrowser(string stockName)
    {
        stockName = stockName.Replace(" ", string.Empty);
        var defaultUri = "http://www.iwencai.com";
        var stockNameUri = $"http://www.iwencai.com/unifiedwap/result?w={stockName}";
        try
        {
            // 通过注册表找到浏览器安装路径
            var browserAppFileName = (string)
                (Registry
                     .GetValue($"HKEY_LOCAL_MACHINE{ApplicationParam.NetBrowserAPPKey}", "", null)
                 ?? Registry
                     .GetValue($"HKEY_CURRENT_USER{ApplicationParam.NetBrowserAPPKey}", "", null));
            // 如果未找到浏览器则使用默认浏览器打开
            if (string.IsNullOrWhiteSpace(browserAppFileName))
            {
                Clipboard.SetDataObject(stockName);
                Process.Start("explorer.exe", defaultUri);
                return;
            }

            // 打开配置的浏览器
            Process.Start(browserAppFileName, stockNameUri);
        }
        catch (Exception ex)
        {
            ex.Message.ErrorMessage();
        }
    }
}